//
//  MarketPaimaiCell.m
//  yigou
//
//  Created by wangdianlei on 2017/1/5.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "MarketPaimaiCell.h"
#import "MarketPaimaiBlock.h"
#import "MMMarketPai.h"
#import "OYCountDownManager.h"

static const NSInteger kViewTag = 4100;

@implementation MarketPaimaiCell

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kCountDownNotification object:nil];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification)    name:kCountDownNotification object:nil];
}

- (void)countDownNotification {
    /// 判断是否需要倒计时 -- 可能有的cell不需要倒计时,根据真实需求来进行判断
    if (0) {
        return;
    }
    
    for(NSUInteger i = 0;  i< self.dataarray.count; i++) {
        MMMarketPai *one = [MARKETDATA.marketRenqi objectAtIndex:i];
        MarketPaimaiBlock *block = [self.scrollview viewWithTag:kViewTag + i];
        NSDateFormatter *date = [[NSDateFormatter alloc] init];
        [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//@"yyyy-MM-dd HH:mm:ss"
        
        NSDate *d = [date dateFromString:one.end_tm];
        NSTimeInterval late = [d timeIntervalSince1970]*1;
        NSDate *d2 = [date dateFromString:one.start_tm];
        NSTimeInterval now = [d2 timeIntervalSince1970]*1;
        
        NSTimeInterval cha = late-now;
    
        /// 计算倒计时
        NSInteger countDown = cha - kCountDownManager.timeInterval;
        if (countDown < 0) return;
        /// 重新赋值
        if (countDown == 0) {
            block.timeline.text = @"拍卖结束";
        } else if (countDown > 86400) {
            block.timeline.text = [NSString stringWithFormat:@"距结束%02zd:%02zd:%02zd:%02zd", (countDown/3600)/24, (countDown/3600)%24, (countDown/60)%60, countDown%60];
            //block.timeline.font = [FontTool fontWithSize:10];
        } else {
            block.timeline.text = [NSString stringWithFormat:@"距结束%02zd:%02zd:%02zd", countDown/3600, (countDown/60)%60, countDown%60];
            //block.timeline.font = [FontTool fontWithSize:12];
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)contentWithArray:(NSMutableArray *)array {
    if(self.scrollview) {
        [self.scrollview removeFromSuperview];
        self.scrollview = nil;
    }
    self.scrollview = [UIScrollView new];
    self.scrollview.backgroundColor = [UIColor whiteColor];
    float widthGap = (ScreenWidth - 109 * 3) / 4 + 109;
    [self.contentView addSubview:self.scrollview];
    self.scrollview.showsHorizontalScrollIndicator = NO;
    NSUInteger numOfBlocks = array.count;
    self.scrollview.contentSize = CGSizeMake(widthGap * numOfBlocks, 0);
    [self.scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.title.mas_bottom).with.offset(15);
        make.height.mas_equalTo(210 - 50 - 15);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
    }];
    
    self.dataarray = array;
    for(NSUInteger i = 0;  i< numOfBlocks; i++) {
        MMMarketPai *one = [array objectAtIndex:i];
        MarketPaimaiBlock *block = [MarketPaimaiBlock new];
        //block.title.text = one.goods_name;
        
        NSString *imgurl = @"";
        imgurl = one.goods_pic_url;
        NSArray *array = [one.goods_pic_url componentsSeparatedByString:@","];
        if (array.count > 0) {
            imgurl = array[0];
        }
        [block.img sd_setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        //block.img.image = [UIImage imageNamed:@"placeholder.png"];
        block.frame = CGRectMake(widthGap*i, 0, widthGap, 70);
        [self.scrollview addSubview:block];
        
        NSString *changeStr = @"当前价: ";
        changeStr = [changeStr stringByAppendingString:@"¥"];
        changeStr = [changeStr stringByAppendingString:one.last_auction_price];
        //NSString *changeStr = one.last_auction_price;
        NSString *subStr = @"当前价: ";
        NSRange range = [changeStr rangeOfString:subStr];
        NSMutableAttributedString *strAttribute = [[NSMutableAttributedString alloc] initWithString:changeStr];
        [strAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:range];
        [strAttribute addAttribute:NSFontAttributeName value:[FontTool fontWithSize:11] range:range];
        block.price.attributedText = strAttribute;
        
        NSDateFormatter *date=[[NSDateFormatter alloc] init];
        [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//@"yyyy-MM-dd HH:mm:ss"
        NSDate *d = [date dateFromString:one.end_tm];
        
        NSTimeInterval late = [d timeIntervalSince1970]*1;
        
        NSDate *d2 = [date dateFromString:one.start_tm];
        NSTimeInterval now = [d2 timeIntervalSince1970]*1;
        
        block.tag = kViewTag + i;
        block.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
        tap.numberOfTapsRequired = 1;
        [block addGestureRecognizer:tap];
    }
}

- (void)click:(UITapGestureRecognizer*)recognizer {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickMarketPaimaiOne:title:)]) {
        [self.delegate clickMarketPaimaiOne:recognizer.view.tag - kViewTag title:self.title.text];
    }
}

ARRAY_INIT(dataarray)

@end
